package com.c.ctgproject.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.entity.MaterialSellEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.ui.sell.material.SaleApplyActivity
import com.c.ctgproject.utils.handlerNull
import org.jetbrains.anko.startActivity

/**
 * 作者:LiBW
 * 创建日期: 2020/9/7
 * 描述:物料销售列表适配器
 */
class MaterialSellListAdapter(private val context: Context, private val mList: List<MaterialSellEntity.ListBean>) : RecyclerView.Adapter<MaterialSellListAdapter.ViewHolder>() {
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mMaterialNameTV: TextView = itemView.findViewById(R.id.mPartNameTV)
        val mMaterialInventoryWeightTV: TextView = itemView.findViewById(R.id.mMaterialInventoryWeightTV)
        val mMaterialNumberTV: TextView = itemView.findViewById(R.id.mMaterialNumberTV)
        val mSellBTN: Button = itemView.findViewById(R.id.mSellBTN)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_material_sell_list, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = mList[position]
        holder.mMaterialNameTV.text = data.materialName.handlerNull()
        holder.mMaterialInventoryWeightTV.text = data.materialInventoryWeight.handlerNull()
        holder.mMaterialNumberTV.text = data.materialInventoryNumber.handlerNull()
        holder.mSellBTN.setOnClickListener {
            context.startActivity<SaleApplyActivity>(
                MyConstants.MATERIAL_ID to data.materialId,
                MyConstants.MATERIAL_NAME to data.materialName,
                MyConstants.MATERIAL_WEIGHT to data.materialInventoryWeight.handlerNull(),
                MyConstants.MATERIAL_COUNT to data.materialInventoryNumber
            )
        }
    }
}